/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月14日         luwei
 */

package com.coocaa.app.gamecenter.framework.ui;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.coocaa.x.uipackage.BaseView;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SkyConstants;

public class GameCenterStatusBar extends BaseView {
    private ImageView mNetPoint = null;
    private ImageView mHandShankPoint = null;
    private Context mContext = null;
    private ImageView mProgressBar = null;
    private float mPart = 0;
    private float mTempPart = 0;
    private int mTransLength = Div(90);

    public GameCenterStatusBar(Context context) {
        super(context);
        this.mContext = context;
        LinearLayout layout = new LinearLayout(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER);
        layout.setOrientation(LinearLayout.VERTICAL);
        enableFocusable(false);
        layout.setGravity(Gravity.CENTER_HORIZONTAL);
        addView(layout, params);
        addNetPointView(layout);
        addHandShankPointView(layout);

    }

    private void addNetPointView(LinearLayout layout) {
        mNetPoint = new ImageView(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                Div(50), Div(40));
        params.topMargin = SkyConstants.Div(10);
        layout.addView(mNetPoint, params);
        mNetPoint.setBackgroundResource(R.drawable.game_center_wifi_on_point);
    }

    private void addHandShankPointView(LinearLayout layout) {
        mHandShankPoint = new ImageView(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                Div(50), Div(50));
        params.topMargin = SkyConstants.Div(10);
        layout.addView(mHandShankPoint, params);
        mHandShankPoint.setScaleType(ScaleType.FIT_CENTER);
        mHandShankPoint.setBackgroundResource(R.drawable.game_center_icon_handshank);
    }


    public void initProgressbar(float part) {
        int height = (int) (Div(750) / part) + mTransLength;
        this.mPart = part;
        this.mTempPart = part;
        if (mProgressBar == null) {
            mProgressBar = new ImageView(mContext);
            mProgressBar.setBackgroundResource(R.drawable.game_center_progress_bar);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, height, Gravity.LEFT);
            params.topMargin = Div(200);
            params.leftMargin = Div(31);
            addView(mProgressBar, params);
        } else {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, height, Gravity.LEFT);
            params.topMargin = Div(200);
            params.leftMargin = Div(31);
            mProgressBar.setLayoutParams(params);
        }
    }

    public void moveNext() {
        if (mTempPart > 1) {
            mTempPart--;
            FrameLayout.LayoutParams params = (LayoutParams) mProgressBar.getLayoutParams();
            params.topMargin = (int) (params.topMargin + mProgressBar.getHeight()) - mTransLength;
            mProgressBar.setLayoutParams(params);
        } else {
            FrameLayout.LayoutParams params = (LayoutParams) mProgressBar.getLayoutParams();
            params.topMargin = (int) (params.topMargin + mTempPart * mProgressBar.getHeight()) - mTransLength;
            mProgressBar.setLayoutParams(params);
        }
    }

    public void movePre() {
        if (mTempPart < 1) {
            FrameLayout.LayoutParams params = (LayoutParams) mProgressBar.getLayoutParams();
            params.topMargin = (int) (params.topMargin - mTempPart * mProgressBar.getHeight()) + mTransLength;
            mProgressBar.setLayoutParams(params);
        } else if (mTempPart < mPart) {
            FrameLayout.LayoutParams params = (LayoutParams) mProgressBar.getLayoutParams();
            params.topMargin = (int) (params.topMargin - mProgressBar.getHeight()) + mTransLength;
            mTempPart++;
            mProgressBar.setLayoutParams(params);
        }
    }

    public void updateNetPoint(boolean on) {
        if (on) {
            mNetPoint.setBackgroundResource(R.drawable.game_center_wifi_on_point);
        } else {
            mNetPoint.setBackgroundResource(R.drawable.game_center_wifi_off_point);
        }
    }

    public void updateHandShankPoint(int num) {
        if (num <= 0) {
            mHandShankPoint.setVisibility(View.GONE);
        } else {
            mHandShankPoint.setVisibility(View.VISIBLE);
        }
    }
}
